package top.hmtools.ckfinder.enums;

import java.util.HashSet;

import top.hmtools.ckfinder.beans.commons.BaseResultBean;
import top.hmtools.ckfinder.command.EmptyCommand;
import top.hmtools.ckfinder.command.SimpleCopyFilesCommand;
import top.hmtools.ckfinder.command.SimpleCreateFolderCommand;
import top.hmtools.ckfinder.command.SimpleDeleteFileCommand;
import top.hmtools.ckfinder.command.SimpleDeleteFolderCommand;
import top.hmtools.ckfinder.command.SimpleDownloadFileCommand;
import top.hmtools.ckfinder.command.SimpleFileUploadCommand;
import top.hmtools.ckfinder.command.SimpleGetFilesCommand;
import top.hmtools.ckfinder.command.SimpleGetFoldersCommand;
import top.hmtools.ckfinder.command.SimpleInitCommand;
import top.hmtools.ckfinder.command.SimpleMoveFilesCommand;
import top.hmtools.ckfinder.command.SimpleQuickUploadCommand;
import top.hmtools.ckfinder.command.SimpleRenameFileCommand;
import top.hmtools.ckfinder.command.SimpleRenameFolderCommand;
import top.hmtools.ckfinder.command.SimpleThumbnailCommand;
import top.hmtools.ckfinder.command.base.BaseCommand;

/**
 * “操作指令”与具体指令执行实现类的关联枚举
 * <br>非常重要的核心之一。
 * @author Hybomyth
 *
 */
public enum ECommandHandler {

		/**
		 * init command.
		 */
		INIT(new SimpleInitCommand()),
		/**
		 * get subfolders for selected location command.
		 */
		GETFOLDERS(new SimpleGetFoldersCommand()),
		/**
		 * get files from current folder command.
		 */
		GETFILES(new SimpleGetFilesCommand()),
		
		/**
		 * get thumbnail for file command.
		 */
		THUMBNAIL(new SimpleThumbnailCommand()),
		/**
		 * download file command.
		 */
		DOWNLOADFILE(new SimpleDownloadFileCommand()),
		/**
		 * create subfolder.
		 */
		CREATEFOLDER(new SimpleCreateFolderCommand()),
		/**
		 * rename file.
		 */
		RENAMEFILE(new SimpleRenameFileCommand()),
		/**
		 * rename folder.
		 */
		RENAMEFOLDER(new SimpleRenameFolderCommand()),
		/**
		 * delete folder.
		 */
		DELETEFOLDER(new SimpleDeleteFolderCommand()),
		/**
		 * copy files.
		 */
		COPYFILES(new SimpleCopyFilesCommand()),
		/**
		 * move files.
		 */
		MOVEFILES(new SimpleMoveFilesCommand()),
		/**
		 * delete files.
		 */
		DELETEFILES(new SimpleDeleteFileCommand()),
		/**
		 * file upload.
		 */
		FILEUPLOAD(new SimpleFileUploadCommand()),
		/**
		 * quick file upload.
		 */
		QUICKUPLOAD(new SimpleQuickUploadCommand()),
		/**
		 * XML error command.
		 */
		XMLERROR(new EmptyCommand()),
		
		/**
		 * error command.
		 */
		ERROR(new EmptyCommand());
		/**
		 * command class for enum field.
		 */
		private BaseCommand<? extends BaseResultBean> command;
		/**
		 * {@code Set} holding enumeration values,
		 */
		private static final HashSet<String> enumValues = new HashSet<String>();

		/**
		 * Enum constructor to set command.
		 *
		 * @param command1 command name
		 */
		private ECommandHandler(final BaseCommand<? extends BaseResultBean> command1) {
			this.command = command1;
		}

		/**
		 * Fills in {@code Set} holding enumeration values for this {@code Enum}.
		 */
		private static void setEnums() {
			for (ECommandHandler enumValue : ECommandHandler.values()) {
				enumValues.add(enumValue.name());
			}
		}

		/**
		 * Checks whether enumeration object contains command name specified as parameter.
		 *请求参数中的命令名称是否被包含在本程序支持的命令集中
		 * @param enumValue string representing command name to check
		 *
		 * @return {@code true} is command exists, {@code false} otherwise
		 */
		public static boolean contains(String enumValue) {
			if (enumValues.isEmpty()) {
				setEnums();
			}
			for (String value : enumValues) {
				if (value.equals(enumValue)) {
					return true;
				}
			}
			return false;
		}

		/**
		 * Executes command.
		 *
		 * @param request request
		 * @param response response
		 * @param configuration connector configuration
		 * @param sc servletContext
		 * @param params params for command.
		 * @throws ConnectorException when error occurs
		 */
//		private void execute(final HttpServletRequest request,
//			final HttpServletResponse response, final IConfiguration configuration,
//			final ServletContext sc, final Object... params)
//			throws ConnectorException {
//			Command com = null;
//			try {
//				//获取具体的命令对象实例？？
//				com = command.getClass().newInstance();
//			} catch (IllegalAccessException e1) {
//				throw new ConnectorException(
//					Constants.Errors.CKFINDER_CONNECTOR_ERROR_INVALID_COMMAND);
//			} catch (InstantiationException e1) {
//				throw new ConnectorException(
//					Constants.Errors.CKFINDER_CONNECTOR_ERROR_INVALID_COMMAND);
//			}
//			if (com == null) {
//				throw new ConnectorException(
//					Constants.Errors.CKFINDER_CONNECTOR_ERROR_INVALID_COMMAND);
//			}
//			com.runCommand(request, response, configuration, params);
//		}

		/**
		 * gets command.
		 *
		 * @return command
		 */
		public BaseCommand<? extends BaseResultBean> getCommand() {
			return this.command;
		}
		
		public void setCommand(BaseCommand<? extends BaseResultBean> cmd){
			this.command = cmd;
		}
	}